package cn.liyunqi.ttv.start.localdb;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * XML based config file help class
 * <p>
 * Created by Owen on 6/16/16.
 */
public class ConfigHelper {

    public static final String LOGIN_ID = "loginId";
    public static final String PASSWORD = "password";
    public static final String IS_SAVE_PASSWORD = "isSavePassword";

    public static final String URL = "url";

    private static final Logger _LOG = LoggerFactory.getLogger(ConfigHelper.class);
    private static final String BASE_DIR = "config";
    private static final String CONFIG_FILE = "/sqlite3.db";


    public static void createEmptyFiles() throws Exception {
        File file = new File(BASE_DIR);
        if (!file.exists()) {
            file.mkdir();
        }
        File uiConfigFile = new File(BASE_DIR + CONFIG_FILE);
        if (!uiConfigFile.exists()) {
            createEmptyXMLFile(uiConfigFile);
        }
    }


    static void createEmptyXMLFile(File uiConfigFile) throws IOException {
        InputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = Thread.currentThread().getContextClassLoader().getResourceAsStream("db/sqlite3.db");
            fos = new FileOutputStream(uiConfigFile);
            byte[] buffer = new byte[1024];
            int byteread = 0;
            while ((byteread = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, byteread);
            }
        } finally {
            if (fis != null) fis.close();
            if (fos != null) fos.close();
        }

    }
    public static String getloadValue(String name) throws Exception {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        try {
            conn = ConnectionManager.getConnection();

            stat = conn.prepareStatement("SELECT * from dbs where name = ?");
            stat.setString(1,name);

            rs = stat.executeQuery();
            while (rs.next()) {
                String value = rs.getString("value");
                return value;
            }
            return null;
        } finally {
            if (rs != null) rs.close();
            if (stat != null) stat.close();
            if (conn != null) conn.close();
        }
    }
    public static void execute(String sql) throws Exception {
        Connection conn = null;
        PreparedStatement stat1 = null;

        try {
            conn = ConnectionManager.getConnection();
            stat1 = conn.prepareStatement(sql);

            stat1.execute();

        } finally {

            if (stat1 != null) stat1.close();

            if (conn != null) conn.close();
        }
    }

   public static void updateValueByName(String name,String value) throws Exception {
       Connection conn = null;
       PreparedStatement stat1 = null;
       PreparedStatement stat2 = null;
       PreparedStatement stat3 = null;
       ResultSet rs = null;
       try {
           conn = ConnectionManager.getConnection();
           stat1 = conn.prepareStatement("SELECT * from dbs where name = ?");
           stat1.setString(1,name);
           rs = stat1.executeQuery();
           if (rs.next()) {

               stat2 = conn.prepareStatement("UPDATE dbs SET value = ? where name = ?");
               stat2.setString(1,value);
               stat2.setString(2,name);
               stat2.executeUpdate();
               return;
           }
           stat3 = conn.prepareStatement("INSERT INTO dbs values(?, ?)");
           stat3.setString(1,name);
           stat3.setString(2,value);
           stat3.executeUpdate();
       } finally {
           if (rs != null) rs.close();
           if (stat1 != null) stat1.close();
           if (stat2 != null) stat2.close();
           if (stat3 != null) stat3.close();
           if (conn != null) conn.close();
       }
   }

    public static void insertOrUpdateUserInfoTokenByUserId(String id, String token) throws Exception {

        Connection conn = null;
        PreparedStatement stat1 = null;
        PreparedStatement stat2 = null;
        PreparedStatement stat3 = null;
        ResultSet rs = null;
        try {
            conn = ConnectionManager.getConnection();
            stat1 = conn.prepareStatement("SELECT * from local_user_info where user_id = ?");
            stat1.setString(1,id);
            rs = stat1.executeQuery();
            if (rs.next()) {

                stat2 = conn.prepareStatement("UPDATE local_user_info SET token = ? where user_id = ?");
                stat2.setString(1,token);
                stat2.setString(2,id);
                stat2.executeUpdate();
                return;
            }
            stat3 = conn.prepareStatement("INSERT INTO local_user_info(user_id,token) values(?, ?)");
            stat3.setString(1,id);
            stat3.setString(2,token);
            stat3.executeUpdate();
        } finally {
            if (rs != null) rs.close();
            if (stat1 != null) stat1.close();
            if (stat2 != null) stat2.close();
            if (stat3 != null) stat3.close();
            if (conn != null) conn.close();
        }
    }
}
